from datetime import datetime, timedelta
from pytz import utc  # 处理时区
from tortoise.expressions import Q
from app.core.redis_base import delete_keys
from app.core.crud import CRUDBase
from app.models.appointment import AppointmentLock
from app.schemas.appointment import AppointmentLockCreate,AppointmentLockUpdate



class AppointmentLockController(CRUDBase[AppointmentLock, AppointmentLockCreate, AppointmentLockUpdate]):
    def __init__(self):
        super().__init__(model=AppointmentLock)


    async def do_create(self,request, obj_in: AppointmentLockCreate):
        await self.create(obj_in=obj_in)
        await self.do_delete_redis(request)
        return True

    async def do_update(self,request, obj_in: AppointmentLockUpdate):
        await self.update(id=obj_in.id, obj_in=obj_in)
        await self.do_delete_redis(request)
        return True

    async def do_delete(self,request, data_id: int):
        await self.remove(id=data_id)
        await self.do_delete_redis(request)
        return True

    async def do_delete_user(self, request, user_id: int):
        await AppointmentLock.filter(user_id=user_id).delete()
        await self.do_delete_redis(request)
        return True

    async def do_delete_5min(self,request):
        five_minutes_ago = datetime.now() - timedelta(minutes=5)
        print(five_minutes_ago)
        await AppointmentLock.filter(created_at__lt=five_minutes_ago).delete()
        # await self.do_delete_redis(request)
        return True

    @staticmethod
    async def do_delete_redis(request):
        pass



appointment_lock_controller =AppointmentLockController()
